package com.whoisaa.wifitransfer.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

import com.whoisaa.wifitransfer.R;
import com.whoisaa.wifitransfer.base.BaseActivity;
import com.whoisaa.wifitransfer.ui.adapter.ChooseFileAdapter;
import com.whoisaa.wifitransfer.ui.adapter.ViewPagerAdapter;
import com.whoisaa.wifitransfer.ui.fragment.ChooseFileFragment;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Description 选择要发送的文件
 * @Author AA
 * @DateTime 16/4/22 上午11:49
 */
public class ChooseFileActivity extends BaseActivity implements ChooseFileAdapter.OnFileChooseListener{

    @Bind(R.id.tl_choose_file_top)
    TabLayout mTabLayout;
    @Bind(R.id.vp_choose_file)
    ViewPager mViewPager;
    ViewPagerAdapter mAdapter;
    @Bind(R.id.btn_choose_file_preview)
    Button previewBtn;
    @Bind(R.id.btn_choose_file_confirm)
    Button confirmBtn;

    private int mCurTabPostion = 0;
    public ArrayList<String> mSelectedFiles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_file);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        confirmBtn.setClickable(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    private void init() {
        mAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mAdapter.addFragment(ChooseFileFragment.newInstance(0, this), "影音");
        mAdapter.addFragment(ChooseFileFragment.newInstance(1, this), "图片");
        mAdapter.addFragment(ChooseFileFragment.newInstance(2, this), "文档");
        mAdapter.addFragment(ChooseFileFragment.newInstance(3, this), "应用");
        mAdapter.addFragment(ChooseFileFragment.newInstance(4, this), "其他");
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mCurTabPostion);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //只有图片才显示预览按钮
                mCurTabPostion = tab.getPosition();
                if(mCurTabPostion == 1) {
                    previewBtn.setVisibility(View.VISIBLE);
                } else {
                    previewBtn.setVisibility(View.GONE);
                }
                mViewPager.setCurrentItem(mCurTabPostion);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @OnClick(R.id.btn_choose_file_preview)
    public void preview() {
        toast("预览");
    }

    @OnClick(R.id.btn_choose_file_confirm)
    public void sendFile(View v) {
        v.setClickable(false);
        Intent intent = new Intent(getAppContext(), SendActivity.class);
        intent.putStringArrayListExtra(SendActivity.KEY_CHOOSEFILES, mSelectedFiles);
        pushActivity(intent);
    }

    @Override
    public void onSelected(String path) {
        mSelectedFiles.add(path);

        //已选中文件,预览和确定按钮可点击
        if(mCurTabPostion == 1) {
            previewBtn.setEnabled(true);
        }
        confirmBtn.setEnabled(true);
    }

    @Override
    public void onUnselected(String path) {
        mSelectedFiles.remove(path);

        //未选中文件,预览和确定按钮不可点击
        if(mSelectedFiles.size() <= 0) {
            if(mCurTabPostion == 1) {
                previewBtn.setEnabled(false);
            }
            confirmBtn.setEnabled(false);
        }
    }
}
